package com.briup.smartcity.web.controller;


import com.briup.smartcity.bean.BaseUser;
import com.briup.smartcity.service.IBaseUserService;
import com.briup.smartcity.utils.Result;
import com.briup.smartcity.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@Api(tags = " 用户管理模块")
@RestController
public class UserController {
    @Autowired
    private IBaseUserService service;

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/auth/user/info")
    public Result findUserInfoByToken(@ApiIgnore @RequestHeader("Authorization") String token, HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
       // System.out.println(authorization);
       // System.out.println(token);

        BaseUser user = service.currentUserInfo(token);
        return ResultUtil.success(user);
    }

    @ApiOperation("分页并模糊查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页",dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",dataType = "int")
    })
    @GetMapping("/auth/user/page/query")
    public Result pageQueryUserInfo(int pageNum, int pageSize, String username, Integer role) {
        //执行查询分页
        PageInfo<BaseUser> pageInfo = service.pageQuery( pageNum,  pageSize, username, role);
        return ResultUtil.success(pageInfo);
    }
    @ApiOperation("更新用户基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dob",value = "出生日期",dataType = "string"),
            @ApiImplicitParam(name = "email",value = "邮箱",dataType = "string"),
            @ApiImplicitParam(name = "gender",value = "性别",dataType = "string"),
            @ApiImplicitParam(name = "realname",value = "姓名",dataType = "string"),
            @ApiImplicitParam(name = "telephone",value = "联系方式",dataType = "string"),
            @ApiImplicitParam(name = "userId",value = "用户编号",dataType = "int")
    })
    @PutMapping("/auth/user/update")
    public Result updateUserInfo(@ApiIgnore @RequestHeader("Authorization")String token,BaseUser baseUser){

        service.updateUser(token,baseUser);//传值
        return ResultUtil.success("更新成功");
    }

    @ApiOperation("更新用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",dataType = "int"),
            @ApiImplicitParam(name = "alterUserIcon",value = "用户头像",dataType = "string")
    })
    @PutMapping("/auth/user/updateUserIcon")
    public Result updateIcon(@ApiIgnore @RequestHeader("Authorization")String token,int id, String userIcon){
        service.updateUserIcon(token,id,userIcon);
        return ResultUtil.success("更新成功");
    }

    @ApiOperation("添加用户")
    @PutMapping("/auth/user/insert")
    public Result insertUser(BaseUser baseUser){
        service.insertUser(baseUser);
        return ResultUtil.success("添加成功");
    }

    @ApiOperation("根据id删除用户信息")
    @DeleteMapping("/auth/user/delete")
    public Result deleteUserById(@ApiIgnore @RequestHeader("Authorization") String token,Integer id){
        service.deleteUserById(token,id);
        return  ResultUtil.success("删除成功");
    }

}